package Tree;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86183
 * Date: 2024-05-29
 * Time: 7:36
 */
public class Tree {
    static class TreeNode{
        public int val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }
    }
    public TreeNode root;
    public TreeNode createTree() {
     TreeNode A = new TreeNode(5);
     TreeNode B = new TreeNode(4);
     TreeNode C = new TreeNode(6);
     TreeNode D = new TreeNode(3);
     TreeNode E = new TreeNode(7);
     A.left = B ;
     A.right = C;
     C.left = D;
     C.right = E;
     return A;
    }
//    public int size(TreeNode root){
//        if(root == null){
//            return 0;
//        }
//        return size(root.left)+ size(root.right)+1;
//    }
//   public int count ;
//    public void size2(TreeNode root){
//        if(root == null){
//            return;
//        }
//            count ++;
//        size2(root.left);
//        size2(root.right);
//    }
//    public void pre(TreeNode root){
//        if(root == null){
//            return;
//        }
//        if(root.left == null && root.right == null){
//            System.out.print(root.val+" ");
//        }
//        pre(root.left);
//        pre(root.right);
//    }
//    public int getHeight(TreeNode root){
//        if(root == null) {
//            return 0;
//        }
//        int leftH  =  getHeight(root.left);
//        int rightH = getHeight(root.right);
//        return Math.max(leftH,rightH)+1;
//    }
    public boolean isValidBST(TreeNode root) {
        if(root == null){
            return true;
        }

        if( root.left!= null && root.val <= root.left.val|| root.right != null &&  root.val >= root.right.val  ){
            return false;
        }
        return isValidBST(root.left) && isValidBST(root.right);

    }
}
